<?php
/**
 * Created by PhpStorm.
 * User: bzg
 * Date: 2019/11/29
 * Time: 17:16
 */

namespace App\Common\Middleware\SysMiddleware;


use App\Extend\SysExtend\Middleware\MiddlewareInterface;
use App\Extend\SysExtend\Token\Token;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;

/**
 * token验证中间件
 * Class TokenCheck
 * @package App\Common\Middleware\SysMiddleware
 */
class TokenCheck implements MiddlewareInterface
{

    /**
     * 控制方法执行前执行的方法
     * @param Request $request
     * @param Response $response
     * @return mixed
     */
    public function before(Request $request, Response $response)
    {
        // TODO: Implement before() method.
        $token = $request->getRequestParam('token');
        $re = Token::getInstance()->check($token);
        if(!is_array($re)) {
            $response->write(json_encode(['code'=>0,'msg'=>$re],JSON_UNESCAPED_UNICODE));
            return false;
        }
    }

    /**
     * 控制方法执行后执行的方法
     * @param Request $request
     * @param Response $response
     * @return mixed
     */
    public function after(Request $request, Response $response)
    {
        // TODO: Implement after() method.
    }
}